/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package multiTabs;

import Tree.DirTree;
import core.IView;
import core.StaticCommand;
import gui.mainFrame;
import java.awt.Color;
import java.awt.Component;
import java.io.File;
import javax.swing.BorderFactory;
import javax.swing.JComponent;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import view.TableDetailView;

/**
 *
 * @author Alex
 */
public class Tab extends javax.swing.JPanel {

    // Tab left
    private IView _viewLeft;
    private JScrollPane _scrollPanelLeft;
    private DirTree _treeLeft;
    
    // Tab right
    private IView _viewRight;
    private JScrollPane _scrollPanelRight;
    private DirTree _treeRight;
    
    
    private MultiTabs _pnlMultiTabs;
    /**
     * Creates new form Tab
     */
    public Tab() {
        initComponents();
    }

    public Tab(MultiTabs multiTabs)
    {
        initComponents();
        _pnlMultiTabs = multiTabs;
        LoadResource();
        CreateDefaultUI();
    }
    
    /**
     * 
     */
    void LoadResource()
    {
        _scrollPanelLeft = new JScrollPane();
        _scrollPanelRight = new JScrollPane();
        
        splitPnl.setOneTouchExpandable(true);
        splitPnl.setDividerLocation(0.5);
        splitPnl.setResizeWeight(0.5);                        
        splitPnl.setLeftComponent(_scrollPanelLeft);
        splitPnl.setRightComponent(_scrollPanelRight);        
        Component[] comps = splitPnl.getComponents();
        for (Component c : comps)
        {
            c.setBackground(Color.WHITE);
        }
    }
    
    void CreateDefaultUI()
    {
        //init view left
        _viewLeft = new TableDetailView(new File("."), true);
        _viewLeft.gotoRoots();
        _scrollPanelLeft.setViewportView((Component) _viewLeft);
        
        //((JComponent) _viewLeft).addMouseListener(_pnlMultiTabs.getMyFocusListenerLeft());

        //_viewLeft.addTransferHandler(_pnlMultiTabs.getTransferHandler());

        //init view right
        _viewRight = new TableDetailView(new File("."), false);
        _viewRight.gotoRoots();
        _scrollPanelRight.setViewportView((Component) _viewRight);
        
        //((JComponent) _viewRight).addMouseListener(_pnlMultiTabs.getMyFocusListenerRight());

        //_viewRight.addTransferHandler(_pnlMultiTab.getTransferHandler());
        
        
//        _viewLeft.addDirChangeListener(_pnlMultiTab.getDirChangeListener());
//        _viewRight.addDirChangeListener(_pnlMultiTab.getDirChangeListener());
    }
    
    
    public IView getViewLeft()
    {
        return _viewLeft;
    }

    public IView getViewRight()
    {
        return _viewRight;
    }    

     /**
     * Xác định panel nào là panel chính
     * @return : trả về LEFT_IS_ACTIVE nếu panel bên trái đang focus,
     * trả về RIGHT_IS_ACTIVE nếu panel bên phải đang focus
     */
    byte getActiveView()
    {
        if (_viewLeft.isFocus())
        {
            return mainFrame.LEFT_IS_ACTIVE;
        }
        return mainFrame.RIGHT_IS_ACTIVE;
    }

    public IView getViewActive()
    {
        if (_viewLeft.isFocus() == true)
        {
            return _viewLeft;
        }
        return _viewRight;
    }

    public JScrollPane getLeftScrollPane()
    {
        return _scrollPanelLeft;
    }

    public JScrollPane getRightScrollPane()
    {
        return _scrollPanelRight;
    }

    JScrollPane getScrollPaneActive()
    {
        if (_viewLeft.isFocus())
        {
            return _scrollPanelLeft;
        }
        return _scrollPanelRight;
    }
    
    public void setLeftFocus()
    {
        _viewLeft.setFocus(true);
        _viewRight.setFocus(false);
        _scrollPanelLeft.setBorder(BorderFactory.createLineBorder(Color.RED, 2));
        _scrollPanelRight.setBorder(BorderFactory.createLineBorder(Color.BLACK, 1));
    }

    public void setRightFocus()
    {
        _viewLeft.setFocus(false);
        _viewRight.setFocus(true);
        _scrollPanelRight.setBorder(BorderFactory.createLineBorder(Color.RED, 2));
        _scrollPanelLeft.setBorder(BorderFactory.createLineBorder(Color.BLACK, 1));
    }
    
    
    
    
    
    
    
    
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        splitPnl = new javax.swing.JSplitPane();

        splitPnl.setName("splitPnl"); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(splitPnl, javax.swing.GroupLayout.DEFAULT_SIZE, 400, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(splitPnl, javax.swing.GroupLayout.DEFAULT_SIZE, 300, Short.MAX_VALUE)
        );
    }// </editor-fold>//GEN-END:initComponents


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JSplitPane splitPnl;
    // End of variables declaration//GEN-END:variables

    public void doViewBrief()
    {
        if (getActiveView() == mainFrame.LEFT_IS_ACTIVE)
        {
            _viewLeft = StaticCommand.createView(StaticCommand.BRIEF_VIEW, _pnlMultiTabs);
        }
        else
        {
            _viewRight = StaticCommand.createView(StaticCommand.BRIEF_VIEW, _pnlMultiTabs);
        }      
    }

    void doViewThumbnail()
    {
        if (getActiveView() == mainFrame.LEFT_IS_ACTIVE)
        {
            _viewLeft = StaticCommand.createView(StaticCommand.THUMBNAIL_VIEW, _pnlMultiTabs);
        }
        else
        {
            _viewRight = StaticCommand.createView(StaticCommand.THUMBNAIL_VIEW, _pnlMultiTabs);
        }        
    }

    void doViewDetail()
    {
        if (getActiveView() == mainFrame.LEFT_IS_ACTIVE)
        {
            _viewLeft = StaticCommand.createView(StaticCommand.DETAILS_VIEW, _pnlMultiTabs);
        }
        else
        {
            _viewRight = StaticCommand.createView(StaticCommand.DETAILS_VIEW, _pnlMultiTabs);
        }        
    }

    public void showTree(byte type)
    {
        if (type == mainFrame.TREE_TYPE_NONE)
        {
            _viewLeft.removeTree();
            _viewRight.removeTree();
            removeTreeLeft();
            removeTreeRight();
        }
        else if (type == mainFrame.TREE_TYPE_ONE)
        {
            createTreeLeft();
            _viewLeft.addTree(_treeLeft);
            _viewRight.addTree(_treeLeft);

            removeTreeRight();
        }
        else if (type == mainFrame.TREE_TYPE_BOTH)
        {
            createTreeLeft();
            _viewLeft.addTree(_treeLeft);
            createTreeRight();
            _viewRight.addTree(_treeRight);
        }

    }

    public void removeTreeLeft()
    {
        if (_treeLeft == null)
        {
            return;
        }
        splitPnl.setLeftComponent(_scrollPanelLeft);

        _treeLeft = null;
        this.repaint();
    }

    public void removeTreeRight()
    {
        if (_treeRight == null)
        {
            return;
        }
        splitPnl.setRightComponent(_scrollPanelRight);
        _treeRight = null;
        this.repaint();
    }
    
    
    public void createTreeLeft()
    {
        if (_treeLeft != null)
        {
            return;
        }
        
        _treeLeft = new DirTree();
        _treeLeft.addTreeSelectionListener(_pnlMultiTabs.getTreeDirSelectionListener());

        JSplitPane splitPaneTree = new JSplitPane();

        splitPaneTree.setOneTouchExpandable(true);
        splitPaneTree.setDividerLocation(0.2);
        splitPaneTree.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        splitPaneTree.setDividerLocation(100);
        splitPaneTree.setDividerSize(5);
        splitPaneTree.setResizeWeight(0.5);
        splitPaneTree.setName("splitPaneTreeLeft");

        JScrollPane scroll = new JScrollPane(_treeLeft);
        splitPaneTree.setLeftComponent(scroll);
        splitPaneTree.setRightComponent(_scrollPanelLeft);

        splitPnl.setLeftComponent(splitPaneTree);
        this.repaint();
    }

    public void createTreeRight()
    {
        if (_treeRight != null)
        {
            return;
        }
        
        _treeRight = new DirTree();
        _treeRight.addTreeSelectionListener(_pnlMultiTabs.getTreeDirSelectionListener());        

        JSplitPane splitPaneTree = new JSplitPane();

        splitPaneTree.setOneTouchExpandable(true);
        splitPaneTree.setDividerLocation(0.2);
        splitPaneTree.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        splitPaneTree.setDividerLocation(100);
        splitPaneTree.setDividerSize(5);
        splitPaneTree.setResizeWeight(0.5);
        splitPaneTree.setName("splitPaneTreeRight");

        JScrollPane scroll = new JScrollPane(_treeRight);
        splitPaneTree.setLeftComponent(scroll);
        splitPaneTree.setRightComponent(_scrollPanelRight);

        splitPnl.setRightComponent(splitPaneTree);
        this.repaint();
    }


}
